<template>
<div>
<el-button type="primary" @click="dialogVisible=true">增加</el-button>
<!-- sync控制 -->
  <el-dialog
  title="增加"
  :visible.sync="dialogVisible"
  width="30%"
  >
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
 <el-form-item label="姓名" prop="name">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
  <el-form-item label="性别" >
  <el-radio-group v-model="ruleForm.gender">
    <el-radio label="男">男</el-radio>
    <el-radio label="女">女</el-radio>
  </el-radio-group>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">取消</el-button>
  </el-form-item>
  </el-form>
  
</el-dialog>
</div>
</template>

<script>
import {add} from '../service'
import {mapActions} from 'vuex'
export default {
   data(){
       return {
        dialogVisible:false,
        ruleForm: {
          name: '',
          gender: ''
        },
        rules: {
          name: [
            { required:true, trigger: 'blur' }
          ],
          age: [
            { required:true, trigger: 'blur' }
          ],
        }
   }
  },
  methods: {
      //this.$refs获取元素的，页面组件的方法
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            await add(this.ruleForm);
            this.dialogVisible=false;
            this.setStudents();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
       ...mapActions("student",["setStudents"])
    }
}
</script>

<style>

</style>
